const {
  $Toast
} = require('../../dist/base/index');
const server = require("../../utils/server.js");
var app = getApp();
Page({
  data: {
    phone: '',
    password: '',
    securityCode: '',
    password2: '',
    currentTime: 60,
    formStatus: false,
    time: "获取验证码",
    vCode: false,
    disabled: true,
    UUID: "Q1W2E3R4T5Y6U7I8O9PASDFGHJKL"
  },
  onLoad: function() {
    let id = wx.getStorageSync("uid");
    if (id) {

      wx.switchTab({
        url: '../index/index',
      })
    } else {

    }
  },
  // 获取输入验证码
  codeInput: function(e) {

    this.setData({
      securityCode: e.detail.value
    })
  },
  // 获取输入账号 
  phoneInput: function(e) {
    this.setData({
      phone: e.detail.value
    })
  },

  // 获取输入密码 
  passwordInput: function(e) {
    this.setData({
      password: e.detail.value
    })
  },
  // 获取输入密码 
  password2Input: function(e) {
    this.setData({
      password2: e.detail.value
    })
  },

  // 提交
  pwdSubmit: function() {

    var self = this;
    self.vertifyCode(this.data.securityCode);
    
  },
  submit() {
    let self = this;
    if (!this.data.phone || !this.data.password || !this.data.password2 || !this.data.securityCode) {
      this.handleText("请正确填写");
      return false;

    } else if (this.data.password2 != this.data.password) {
      this.handleText("密码不一致");
      return false;
    } else {
      server.postReq("/mobile/forgetPassword", {
        "phone": self.data.phone,
        "newPassWord": self.data.password,
        "affirmPassWord": self.data.password2
      }, function (res) {
        console.log(res);
        if (res.data.code == 1) {
          self.handleSuccess("修改成功！");
          setTimeout(function () {
            wx.navigateBack({
              url: '../login/login'
            })
          }, 1000)

        }
      })

    }

  },
  vertifyCode(code) {
    let that = this;
    let flag = false;

    server.postReq('/mobile/SelectVerify', {
      phone: that.data.phone,
      securityCode: that.data.securityCode
    }, function(res) {
      that.handleText(res.data.msg);
      if (res.data.code == 1) {
        that.submit();
       

      } else {
        that.setData({
          formStatus: false,
        })
        flag = false;

      }
    })
 
  },
  sendCode() {
    let that = this;
    var phoneReg = /^1[3-9]\d{9}$/;
    if (!phoneReg.test(this.data.phone)) {
      that.handleWarning("手机号输入不正确");
      return false;
    }

    server.postReq('/mobile/forgetAcquire', {
      phone: that.data.phone,
      UUID: that.data.UUID
    }, function(res) {
      that.handleSuccess(res.data.msg)
      if (res.data.code == 1) {

      }
    })
  },
  getCode: function(options) {

    var that = this;
    var currentTime = that.data.currentTime;
    if (!that.data.phone) {
      that.handleWarning('请输入手机号');
      return false;
    };
    that.sendCode();
    that.setData({
      time: currentTime + '秒'
    })
    var interval = setInterval(function() {
      that.setData({
        time: (currentTime - 1) + '秒',
        disabled: false,
      })
      currentTime--;
      if (currentTime <= 0) {
        clearInterval(interval)
        that.setData({
          time: '重新获取',
          currentTime: 60,
          disabled: true
        })
      }
    }, 1000)
  },

  handleText(msg) {
    $Toast({
      content: msg
    });
  },
  handleSuccess(successMsg) {
    $Toast({
      content: successMsg,
      type: 'success'
    });
  },
  handleWarning(msg) {
    $Toast({
      content: msg,
      type: 'warning'
    });
  },
  handleError() {
    $Toast({
      content: '错误的提示',
      type: 'error'
    });
  },
  handleLoading() {
    $Toast({
      content: '加载中',
      type: 'loading'
    });
  },
})